package com.zuipin.controller;

import java.text.ParseException;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IMemberService;
import com.zuipin.service.IThirdAccountService;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.MemberInfoVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: LoginController
 * @author wuyicheng
 * @time 2017年3月27日下午4:11:16
 */
@RestController
@Api(value = "登录模块", description = "登录模块-wuyicheng")
@RequestMapping(value = "/login")
public class LoginController {
	@Resource
	private IMemberService			memberService;
	@Resource
	private IMemberAccountService	memberAccountService;
	@Resource
	private IThirdAccountService	thirdAccountService;
	@Value("${sms.account}")
	private String					smsAccount;
	@Value("${sms.password}")
	private String					smsPassword;
	@Value("${sms.sendFlag}")
	private String					sendFlag;
	private static Logger			log	= LoggerFactory.getLogger(LoginController.class);
	
	// @ResponseBody
	@RequestMapping(value = "/doSendSms", method = RequestMethod.GET)
	@ApiOperation(value = "发送短信验证码-wuyicheng", notes = "发送短信验证码-wuyicheng")
	@Valids({ @Valid(name = "mobilePhone", regex = RegexUtils.MOBILE_FUZZY) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "mobilePhone", value = "会员手机号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sendType", value = "发送类型", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Map<String, Object>> doSendSms(@RequestParam String mobilePhone, @RequestParam(required = false) String sendType, @RequestParam Long sysId)
			throws ParseException {
		Result<Map<String, Object>> result = new Result<>();
		result = memberService.sendMobileCode(mobilePhone, smsAccount, smsPassword, sendFlag, result, sendType, sysId);
		return result;
	}
	
	// @Json(type = Result.class, exclude = "errorCode,data")
	@RequestMapping(value = "/doRegister", method = RequestMethod.POST)
	@ApiOperation(value = "会员注册-wuyicheng", notes = "会员注册-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>4020-规则不存在" + "</br>7008-会员不存在等级")
	@Valids({ @Valid(name = "memberAccount", regex = RegexUtils.MOBILE_FUZZY), @Valid(name = "password", maxLength = 16, minLength = 6), @Valid(name = "device", regex = "PC|WAP"),
			@Valid(name = "sysId", minValue = 0), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberAccount", value = "会员账号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "平台", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<MemberInfoVo> doRegister(@RequestParam String memberAccount, @RequestParam String password, @RequestParam Long sysId,
			@RequestParam(required = false) String device) throws CustomException, ParseException {
		Result<MemberInfoVo> result = new Result<>();
		result = memberAccountService.doRegister(memberAccount, password, sysId, result, device);
		return result;
	}
	
	// @Jsons({ @Json(type = Result.class, exclude = "errorCode"),
	// @Json(type = MemberInfoVo.class, exclude = "thirdAccount"),
	// @Json(type = Member.class, exclude = "createTime,createId,updateTime,updateId,provinceCode,cityCode,areaCode"),
	// @Json(type = MemberAccount.class, exclude = "createTime,createId,updateTime,updateId,"), })
	@RequestMapping(value = "/doLoginByPwd", method = RequestMethod.POST)
	@ApiOperation(value = "用户密码登录-wuyicheng", notes = "用户密码登录-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>4020-规则不存在" + "</br>7008-会员不存在等级"
			+ "</br>4064-会员被冻结")
	@Valids({ @Valid(name = "memberAccount", regex = RegexUtils.MOBILE_FUZZY + "|" + RegexUtils.EMAIL), @Valid(name = "password", minLength = 6, maxLength = 16),
			@Valid(name = "device", regex = "PC|WAP"), @Valid(name = "sysId", minValue = 0, maxValue = 10) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberAccount", value = "会员账号(邮箱或手机号)", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "平台", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<MemberInfoVo> doLoginByPwd(@RequestParam String memberAccount, @RequestParam String password, @RequestParam Long sysId,
			@RequestParam(required = false) String device) throws CustomException, ParseException {
		Result<MemberInfoVo> result = new Result<>();
		result = memberAccountService.doLoginByPwd(memberAccount, password, sysId, result, device);
		return result;
	}
	
	// @Jsons({ @Json(type = Result.class, exclude = "errorCode"),
	// @Json(type = MemberInfoVo.class, exclude = "thirdAccount"),
	// @Json(type = Member.class, exclude = "createTime,createId,updateTime,updateId,provinceCode,cityCode,areaCode"),
	// @Json(type = MemberAccount.class, exclude = "createTime,createId,updateTime,updateId,"), })
	@RequestMapping(value = "/doLoginBySms", method = RequestMethod.POST)
	@ApiOperation(value = "快速登录接口-wuyicheng", notes = "快速登录接口-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>4020-规则不存在" + "</br>7008-会员不存在等级"
			+ "</br>4064-会员被冻结")
	@Valids({ @Valid(name = "memberAccount", regex = RegexUtils.MOBILE_FUZZY), @Valid(name = "device", regex = "PC|WAP"), @Valid(name = "sysId", minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberAccount", value = "会员账号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "平台", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<MemberInfoVo> doLoginBySms(@RequestParam String memberAccount, @RequestParam Long sysId, @RequestParam(required = false) String device)
			throws CustomException, ParseException {
		Result<MemberInfoVo> result = new Result<>();
		result = memberAccountService.doLoginBySms(memberAccount, sysId, result, device);
		return result;
	}
	
	// @Jsons({ @Json(type = Result.class, exclude = "errorCode"),
	// @Json(type = ThirdAccount.class, exclude = "createTime,createId,updateTime,updateId,"),
	// @Json(type = Member.class, exclude = "createTime,createId,updateTime,updateId,provinceCode,cityCode,areaCode"),
	// @Json(type = MemberAccount.class, exclude = "createTime,createId,updateTime,updateId,"), })
	@RequestMapping(value = "/doLoginByThirdLogin", method = RequestMethod.POST)
	@ApiOperation(value = "第三方登录接口-wuyicheng", notes = "第三方登录接口-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>4020-规则不存在" + "</br>7008-会员不存在等级"
			+ "</br>4064-会员被冻结")
	@Valids({ @Valid(name = "openId", minLength = 0, maxLength = 50), @Valid(name = "platformId", minLength = 0), @Valid(name = "nickName", required = false),
			@Valid(name = "device", regex = "PC|WAP"), @Valid(name = "sysId", minValue = 0) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "openId", value = "第三方平台ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "platformId", value = "平台ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "nickName", value = "第三方平台昵称", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "平台", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "unionId", value = "微信unionId", required = false, dataType = "long", paramType = "query"), })
	public Result<MemberInfoVo> doLoginByThirdLogin(@RequestParam String openId, @RequestParam Long platformId, @RequestParam(required = false) String nickName,
			@RequestParam Long sysId, @RequestParam(required = false) String unionId, @RequestParam(required = false) String device) throws CustomException, ParseException {
		Result<MemberInfoVo> result = new Result<>();
		result = memberAccountService.doLoginByThirdLogin(openId, platformId, nickName, sysId, unionId, result, device);
		return result;
	}
	
}